<template>
  <div class="cart">
    <RouterLink class="curr" to="/cart">
      <i class="iconfont icon-cart"></i>
      <em>{{ $store.getters["cart/validTotal"] }}</em>
    </RouterLink>
    <div
      class="layer"
      v-if="$store.getters['cart/validList'].length && $route.path !== '/cart'"
    >
      <div class="list">
        <div
          class="item"
          v-for="item in $store.getters['cart/validList']"
          :key="item.skuId"
        >
          <RouterLink :to="`/product/${item.id}`">
            <img :src="item.picture" />
            <div class="center">
              <p class="name ellipsis-2">{{ item.name }}</p>
              <p class="attr ellipsis">{{ item.attrsText }}</p>
            </div>
            <div class="right">
              <p class="price">&yen;{{ item.nowPrice }}</p>
              <p class="count">x{{ item.count }}</p>
            </div>
          </RouterLink>
          <i
            @click="deleteCart(item.skuId)"
            class="iconfont icon-close-new"
          ></i>
        </div>
      </div>
      <div class="foot">
        <div class="total">
          <p>共 {{ $store.getters["cart/validTotal"] }} 件商品</p>
          <p>&yen;{{ $store.getters["cart/validAmount"] }}</p>
        </div>
        <XtxButton @click="$router.push('/cart')" type="plain">
          去购物车结算
        </XtxButton>
      </div>
    </div>
  </div>
</template>
<script>
import { onMounted } from "vue-demi";
import { useStore } from "vuex";
import Message from "erabbit-ui/packages/components/Message";
export default {
  name: "AppHeaderCart",
  // 1. 获取最新的购物车信息
  // 1.1 如果是已登录，调用的是购物车列表接口，拿的就是最新的
  // 1.2 未登录状态，通过一个查询商品最新：价格 库存 是否有效 的接口
  // 1.3 通过 mutations 函数更新本地购物车商品对象，updateCart 函数
  // 1.4 发请求的事 放到actions去做，数据有了调用 updateCart mutation函数
  // 1.5 当前组件初始化调用即可
  setup() {
    const store = useStore();
    onMounted(() => {
      store.dispatch("cart/getCartList");
    });

    // 移除商品
    // 1. 写mutations函数，删除state中的对应商品
    // 2. 写actions函数，处理登录和未登录情况，实现未登录调用mutations即可
    // 3. 当前组件绑定删除的点击，分发actions函数即可
    const deleteCart = (skuId) => {
      store
        .dispatch("cart/deleteCart", skuId)
        .then(() => {
          Message({ text: "删除成功", type: "success" });
        })
        .catch(() => {
          Message({ text: "删除失败", type: "error" });
        });
    };
    return { deleteCart };
  },
};
</script>
<style scoped lang="less">
.cart {
  width: 50px;
  position: relative;
  z-index: 600;
  .curr {
    height: 32px;
    line-height: 32px;
    text-align: center;
    position: relative;
    display: block;
    .icon-cart {
      font-size: 22px;
    }
    em {
      font-style: normal;
      position: absolute;
      right: 0;
      top: 0;
      padding: 1px 6px;
      line-height: 1;
      background: var(--help-color);
      color: #fff;
      font-size: 12px;
      border-radius: 10px;
      font-family: Arial;
    }
  }
  &:hover {
    .layer {
      opacity: 1;
      transform: none;
    }
  }
  .layer {
    opacity: 0;
    transition: all 0.4s 0.2s;
    transform: translateY(-200px) scale(1, 0);
    width: 400px;
    height: 400px;
    position: absolute;
    top: 50px;
    right: 0;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    background: #fff;
    border-radius: 4px;
    padding-top: 10px;
    &::before {
      content: "";
      position: absolute;
      right: 14px;
      top: -10px;
      width: 20px;
      height: 20px;
      background: #fff;
      transform: scale(0.6, 1) rotate(45deg);
      box-shadow: -3px -3px 5px rgba(0, 0, 0, 0.1);
    }
    .foot {
      position: absolute;
      left: 0;
      bottom: 0;
      height: 70px;
      width: 100%;
      padding: 10px;
      display: flex;
      justify-content: space-between;
      background: #f8f8f8;
      align-items: center;
      .total {
        padding-left: 10px;
        color: #999;
        p {
          &:last-child {
            font-size: 18px;
            color: var(--price-color);
          }
        }
      }
    }
  }
  .list {
    height: 310px;
    overflow: auto;
    padding: 0 10px;
    &::-webkit-scrollbar {
      width: 10px;
      height: 10px;
    }
    &::-webkit-scrollbar-track {
      background: #f8f8f8;
      border-radius: 2px;
    }
    &::-webkit-scrollbar-thumb {
      background: #eee;
      border-radius: 10px;
    }
    &::-webkit-scrollbar-thumb:hover {
      background: #ccc;
    }
    .item {
      border-bottom: 1px solid #f5f5f5;
      padding: 10px 0;
      position: relative;
      i {
        position: absolute;
        bottom: 38px;
        right: 0;
        opacity: 0;
        color: #666;
        transition: all 0.5s;
      }
      &:hover {
        i {
          opacity: 1;
          cursor: pointer;
        }
      }
      a {
        display: flex;
        align-items: center;
        img {
          height: 80px;
          width: 80px;
        }
        .center {
          padding: 0 10px;
          width: 200px;
          .name {
            font-size: 16px;
          }
          .attr {
            color: #999;
            padding-top: 5px;
          }
        }
        .right {
          width: 100px;
          padding-right: 20px;
          text-align: center;
          .price {
            font-size: 16px;
            color: var(--price-color);
          }
          .count {
            color: #999;
            margin-top: 5px;
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>
